const express = require('express');
const mysqla = require('mysql');
const app = express();
app.use(express.static('www'));
const mysql= mysqla.createConnection({
    host: 'localhost',
    user: 'root',
    password: 'root',
    database: 'yuedu',
});
mysql.connect(function (err) {
    if (err) {
        console.log('连接数据库失败');
    } else {
        console.log('连接数据库成功');
    }
})
app.get('/listref',function (req, res) {
    console.log(req.query);
    var {tab,limit,page} = req.query;
    //page1 1
    //page2  21
    //page3  41
    var num = (page - 1) * limit + 1
    if (tab == 0) {
        var sql = `select * from t_articles limit ${num},${limit}`;
    } else {
        var sql = `select * from t_articles where type_id=${tab} limit ${num},${limit}`;
    }
    mysql.query(sql, function (err,data) {
        if (err) {
res.json({success:0,data:'获取失败'})
        }else{
            res.json({success:1,data})
        }
    })


})
    app.get('/xiang',function (req,res) {
        console.log(req.query);//{id:}
        var {id}=req.query;
        var sql = `select * from t_articles where id=${id}`
        mysql.query(sql,function (err,data) {if (err) {
            res.json({success:0,data:'获取失败'})
        }else{
            res.json({success:1,data})
        }
            
        })
    })




app.listen(3000, function () {
    console.log('http://localhost:3000');
})